package cn.parus.config;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

@org.springframework.context.annotation.Configuration
public class HadoopConfig {

    // HDFS的URI
    @Value("${hadoop.hdfs.uri}")
    private String hdfsUri;
    // HDFS的用户
    @Value("${hadoop.hdfs.user}")
    private String hdfsUser;
    @Value("${hadoop.config.dir}")
    private String hadoopConfigDir;


    // 创建Hadoop配置对象
    @Bean
    public Configuration hadoopConfiguration() {
        Configuration conf = new Configuration();

        // 设置HDFS的URI
        conf.set("fs.defaultFS", hdfsUri);

        // 设置HDFS的用户
        conf.set("hadoop.job.ugi", hdfsUser);

        conf.addResource(new Path(hadoopConfigDir + "core-site.xml"));
        conf.addResource(new Path(hadoopConfigDir + "hdfs-site.xml"));

        return conf;
    }

    // 创建HDFS文件系统对象
    @Bean
    public FileSystem hdfsFileSystem() throws Exception {
        Configuration conf = hadoopConfiguration();
        return FileSystem.get(conf);
    }
}